package com.controller;

import com.bean.Clazz;
import com.bean.Student;
import com.dto.StudentNumAndClazzVo;
import com.service.ClazzService;
import com.service.StudentService;
import com.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClazzService clazzService;
    /**
     *
     * 后台管理首页
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:11
     */

    @RequestMapping(value = "/welcome")
    public String welcome(){
        return "welcome";
    }

    /**
     *
     * 学生列表
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:11
     */

    @RequestMapping(value = "/student_list_admin")
    public String student_list_admin(){
        return "student/student_list_admin";
    }
    /**
     *
     * 分页查询学生列表信息
     * @param page
     * @param limit
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:12
     */

    @RequestMapping(value = "/AdminStudentListServlet")
    @ResponseBody
    public ResultVo AdminStudentListServlet(int page, int limit){
        ResultVo resultVo = new ResultVo();
        List<Student> students=studentService.getStudentsByPage(page,limit);
        int length = studentService.getStudentsLength();
        resultVo.setData(students);
        resultVo.setCount(length);
        resultVo.setMsg("success");
        return resultVo;
    }
    /**
     *
     * 跳转到添加学生页面
     * @param student
     * @param model
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:12
     */

    @RequestMapping(value = "/AdminAddStudentServlet1")
    public String AdminStudentListServlet1(Student student, Model model){
        List<Clazz> clazzes=clazzService.getClazzs();
        model.addAttribute("clazzes",clazzes);
        return "student/add_student";
    }
    /**
     *
     * 添加学生信息
     * @param student
     * @param model
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:12
     */

    @RequestMapping(value = "/AdminAddStudentListServlet2")
    public String AdminAddStudentListServlet2(Student student, Model model){
        studentService.addStudent(student);
        model.addAttribute("students", studentService.getStudents());
        return "main";
    }
    /**
     *
     * 根据学生编号删除学生
     * @param id
     * @param model
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:13
     */

    @RequestMapping(value = "/delStudentById")
    @ResponseBody
    public ResultVo delStudentById(@RequestParam("id") int id, Model model){
        ResultVo resultVo = new ResultVo();
        int i=studentService.delStudentById(id);;
        model.addAttribute("students", studentService.getStudents());
        if(i==1){
        resultVo.setMsg("删除成功");}
        else{
            resultVo.setMsg("删除失败");
        }
        return resultVo;
    }
    /**
     *
     * 跳转到编辑学生页面
     * @param id
     * @param model
     * @return java.lang.String
     * @author 末日流光
     * @date 2022/5/13 18:13
     */

    @RequestMapping(value = "/AdmineditStudentServlet")
    public String AdmineditUserServlet(@RequestParam("id") int id,Model model){
        Student student=studentService.selectStudentById(id);
        model.addAttribute("student",student);
        List<Clazz> clazzes=clazzService.getClazzs();
        model.addAttribute("clazzes",clazzes);
        return "student/edit_student";
    }
    /**
     *
     * 编辑学生信息
     * @param student
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:14
     */


    @RequestMapping(value = "/AdmineditStudentServlet2")
    public ResultVo AdmineditUserServlet2(Student student){
        ResultVo resultVo = new ResultVo();
        int i=studentService.updateStudentById(student);
        if(i==1){
            resultVo.setMsg("编辑成功");}
        else{
            resultVo.setMsg("编辑失败");
        }
        return resultVo;
    }
    /**
     *
     * 批量删除学生信息
     * @param Ids
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:14
     */

    @RequestMapping("deleteStudentBatch")
    @ResponseBody
    public ResultVo deleteBatch(@RequestParam("Ids") String[] Ids) {
        ResultVo resultVo = new ResultVo();
        for(String id:Ids){
            studentService.delStudentById(Integer.parseInt(id));
        }
        resultVo.setData(Ids);
        resultVo.setMsg("删除成功");
        return resultVo;
    }
    /**
     *
     * 图片上传
     * @param file
     * @param request
     * @param model
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:15
     */

    @RequestMapping("/UploadStudentServlet")
    @ResponseBody
    public ResultVo UploaduserServlet( @RequestParam("file") MultipartFile file, HttpServletRequest request,Model model) throws IOException {
        ResultVo resultVo = new ResultVo();
//        System.out.println(file);
        //imgFile表示上传的路径
        //截取上传文件的后缀名,生成新的后缀名
        String oringnalFilename=file.getOriginalFilename();
        String ext=oringnalFilename.substring(oringnalFilename.lastIndexOf("."));
        String fileName=System.currentTimeMillis()+ext;
        //获取imgs目录在服务器的路径
//        String dir=request.getServletContext().getRealPath("upload");
        String dir="D:\\IdeaProject\\SSM-demo1\\src\\main\\webapp\\upload";
        String savePath=dir+"/"+fileName;
//        System.out.println(savePath);
        file.transferTo(new File(savePath));
        resultVo.setData(fileName);
        resultVo.setMsg("上传成功");
        return resultVo;
    }
    /**
     *
     * 根据学生名模糊查询学生信息
     * @param studentName
     * @param page
     * @param limit
     * @return com.utils.ResultVo
     * @author 末日流光
     * @date 2022/5/13 18:15
     */

    @RequestMapping(value = "/selectStudentByStudentName")
    @ResponseBody
    public ResultVo selectStudentByStudentName(@RequestParam("studentName")String studentName,int page, int limit){
       List<Student> students=studentService.selectStudentByName(studentName,page,limit);
        int length = studentService.selectStudentByNameLength(studentName);
       ResultVo resultVo = new ResultVo();
        resultVo.setCount(length);
        resultVo.setData(students);
        resultVo.setMsg("查询成功");
        return resultVo;
    }
    @RequestMapping(value = "/getStudentNumWithClazz")
    @ResponseBody
    public StudentNumAndClazzVo getStudentNumWithClazz(){
        StudentNumAndClazzVo studentNumAndClazzVo=new StudentNumAndClazzVo();
        List<Clazz> clazzes= clazzService.getClazzs();
        List<Integer> students=new ArrayList<>();
        for (Clazz c:clazzes) {
            students.add(studentService.selectStudentBycIdLength(c.getCid()));
        }

        studentNumAndClazzVo.setClazz(clazzes);
        studentNumAndClazzVo.setCount(clazzes.size());
        studentNumAndClazzVo.setStudentNum(students);
        return studentNumAndClazzVo;
    }
}
